package com.zhitong.ztblog.services.servicesImp;

import com.zhitong.ztblog.entity.Blog;
import com.zhitong.ztblog.entity.Label;
import com.zhitong.ztblog.entity.PageInfo;
import com.zhitong.ztblog.mappers.BlogMapper;
import com.zhitong.ztblog.mappers.LabelMapper;
import com.zhitong.ztblog.services.IBlogService;
import com.zhitong.ztblog.services.ILabelService;
import com.zhitong.ztblog.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;

@Service
public class BlogService implements IBlogService {

    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    private ILabelService labelService;

    @Override
    public Blog findById(int id) {
        Blog blog = blogMapper.findById(id);
        return blog;
    }

    @Override
    public Blog[] findAll() {
        Blog[] all = blogMapper.findAll();
        return all;
    }


    @Override
    public int findBlogsNum() {
        int num = blogMapper.findBlogsNum();
        return num;
    }

    @Override
    public Blog[] findByPage(PageInfo pageInfo) {

        if (pageInfo==null){
            Blog[] blogs = blogMapper.findBlogByPage(0, 10);
            return blogs;
        }else{
            Blog[] blogs = blogMapper.findBlogByPage((pageInfo.getCurrentPage() - 1) * pageInfo.getPageSize(), pageInfo.getPageSize());
            return blogs;
        }
    }

    @Override
    public Blog[] findByTop() {
        Blog[] blogByTop = blogMapper.findBlogByTop();
        return blogByTop;
    }

    @Override
    public void updateTotle(int id) {
        blogMapper.updateTotle(id);
    }

    @Override
    public Blog[] findBlogBySid(int sid, PageInfo pageInfo) {
        Blog[] blogs = blogMapper.findBlogBySid(sid, (pageInfo.getCurrentPage() - 1) * pageInfo.getPageSize(), pageInfo.getPageSize());
        return blogs;
    }

    @Override
    public Blog[] findBlogByLid(int lid, PageInfo pageInfo) {
        Blog[] blogs = blogMapper.findBlogBylid(lid, (pageInfo.getCurrentPage() - 1) * pageInfo.getPageSize(), pageInfo.getPageSize());
        return blogs;
    }

    @Override
    public int findBlogNumBySid(int sid) {
        int num = blogMapper.findBlogNumBySid(sid);
        return num;
    }

    @Override
    public void delBlogById(int id) {
        blogMapper.delBlogById(id);
    }

    @Override
    public void insertBlog(Blog blog) {
        blog.setCreateTime(new Date());
        blog.setLastTime(new Date());
        blogMapper.insertBlog(blog);
        String[] labels = blog.getLabels();
        for (String l:labels) {
            labelService.bInsertLabelByName(blog.getId(),l);
        }

    }

    @Override
    public void updateBlog(Blog blog) {
        blogMapper.updateBlog(blog);
        String[] labels = blog.getLabels();
        labelService.deleteByBid(blog.getId());
        for (String n:labels
             ) {
            labelService.bInsertLabelByName(blog.getId(),n);
        }

    }

    @Override
    public Blog[] findBlogByWord(String word, PageInfo pageInfo) {
        Blog[] blogs = blogMapper.findBlogByWord(word, (pageInfo.getCurrentPage() - 1) * pageInfo.getPageSize(), pageInfo.getPageSize());
        return blogs;
    }

    @Override
    public int findBlogNumByWord(String word) {
        int nums = blogMapper.findBlogNumsByWord(word);
        return nums;
    }


}
